package com.quick.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
@SuppressWarnings("unused")
public class DateUtil {

    //
    
	private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    //
    private static final String MONTH_FORMAT = "MM-dd HH:mm";
    //
    private static final String YYYYMMDD_FORMAT = "yyyyMMdd";
    // 精确到秒
    private static final String ZONE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    // 精确到毫秒
    private static final String ZONE_FORMAT_NUMBER = "yyyy-MM-dd HH:mm:ss.SSS";
    // 8小时的秒数
    private static final int OFFSET = 8 * 60 * 60;


    /**
     * 获取秒数
     *
     * @return
     */
    public static Long epochSecond(){
        return  LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * 获取毫秒数
     * @return
     */
    public static Long millisSeconds(){
        return LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     *  格式化日期
     * @param localDateTime
     * @param pattern
     * @return
     */
    public static String format(LocalDateTime localDateTime,String pattern) {
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 时间转换
     * @param localDateTime
     * @return
     */
    public static String translation(LocalDateTime localDateTime) {
        Long now = epochSecond();
        Long time = toEpochSecond(localDateTime);
        Long diff = now - time;
        // 1秒钟以内
        if(diff <= 60){
            return  "刚刚";
        }else if (diff <= 3600){
            return  diff / 60 + "分钟前";
        }else if (diff <= 86400){
            return  diff / 3600 + "小时前";
        }else if (diff <= 604800){
            return  DateUtil.format(localDateTime,MONTH_FORMAT);
        }else if (diff <= 2592000){// 1月以内
            return  DateUtil.format(localDateTime,MONTH_FORMAT);
        }else if (diff <= 31536000){// 1年以内
            return  DateUtil.format(localDateTime,MONTH_FORMAT);
        }else if (diff <= 315360000){
            return  DateUtil.format(localDateTime,"yyyy-MM-dd");
        }else{
            return  "很久以前";
        }
    }

    /**
     *  时间转换为毫秒
     * @param localDateTime
     * @return
     */
    public static Long toMilliSecond(LocalDateTime localDateTime) {
        // 比标准实际慢8小时，就是当前的时间了。
        return localDateTime.toInstant(ZoneOffset.ofTotalSeconds(OFFSET)).toEpochMilli();
    }

    /**
     * 时间转换为秒
     * @param localDateTime
     * @return
     */
    public static Long toEpochSecond(LocalDateTime localDateTime){
        return localDateTime.toInstant(ZoneOffset.ofTotalSeconds(OFFSET)).getEpochSecond();
    }

    /**
     *  字符串时间转换为LocalDateTime
     * @param dateStr
     * @return
     */
    public static LocalDateTime toLocalDateTime(String dateStr){
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(DATE_FORMAT);
        return LocalDateTime.parse(dateStr, fmt);
    }
    
    public static String getWeekStrTime(LocalDateTime interviewStartTime, LocalDateTime interviewEndTime,String joint) {
        if (ObjectUtils.isEmpty(interviewStartTime) || ObjectUtils.isEmpty(interviewEndTime)) {
            return "";
        }
        if(StringUtils.isBlank(joint)){
            joint = "-";
        }

        if(interviewStartTime.getYear() == interviewEndTime.getYear()
                && interviewStartTime.getMonth() == interviewEndTime.getMonth()
                && interviewStartTime.getDayOfMonth() == interviewEndTime.getDayOfMonth()) {

            return formatTimeInt(interviewStartTime.getMonthValue()) + "-" + formatTimeInt(interviewStartTime.getDayOfMonth())
                    + " " + DateUtil.getDayOfWeekDayName(interviewStartTime.toLocalDate())
                    + " " + formatTimeInt(interviewStartTime.getHour()) + ":" + formatTimeInt(interviewStartTime.getMinute())
                    + joint + formatTimeInt(interviewEndTime.getHour()) + ":" + formatTimeInt(interviewEndTime.getMinute());
        }

        return formatTimeInt(interviewStartTime.getMonthValue())+"-"+formatTimeInt(interviewStartTime.getDayOfMonth())
                + " " + DateUtil.getDayOfWeekDayName(interviewStartTime.toLocalDate())
                + " " + formatTimeInt(interviewStartTime.getHour()) + ":" + formatTimeInt(interviewStartTime.getMinute())
                + joint + interviewEndTime.getYear() + joint + formatTimeInt(interviewEndTime.getMonthValue()) + joint + formatTimeInt(interviewEndTime.getDayOfMonth())
                + " " + DateUtil.getDayOfWeekDayName(interviewEndTime.toLocalDate())
                + " " + formatTimeInt(interviewEndTime.getHour()) + ":" + formatTimeInt(interviewEndTime.getMinute());
    }
    
    /**
     * 获取当前周几
     * @param date
     * @return
     */
    public static String getDayOfWeekDayName(LocalDate date) {
        switch (date.getDayOfWeek().getValue() - 1){
            case 0:
                return "周一";
            case 1:
                return "周二";
            case 2:
                return "周三";
            case 3:
                return "周四";
            case 4:
                return "周五";
            case 5:
                return "周六";
            case 6:
                return "周日";
            default:
                return "";
        }
    }
    
    
    private static String formatTimeInt(int number) {
        if (number == 0) {
            return "00";
        } else if (number <= 9) {
            return "0"+number;
        }
        return number+"";
    }

    public static void main(String[] args) throws ParseException {
    	Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;

        List<String> yearMonths = new ArrayList<>();

        for (int month = 1; month <=12; month++) {
            String yearMonth = currentYear + "-" + String.format("%02d", month);
            yearMonths.add(yearMonth);
        }

        for (String yearMonth : yearMonths) {
            System.out.println(yearMonth);
        }
    }
}
